#!/bin/bash
# $Id: MAKECD 774 2010-11-09 01:18:31Z ant_code $

set -eETa
set -o pipefail
. ./files/env
. ./files/buildscripts.fn
trap '_chk_err' INT HUP TERM ERR # not very usefull, but
export -f _chk_err

export wd=`pwd`

make_auxbuildroot ()
{
	./bin/mk-dir-struct.sh
	./bin/fetch-src
	non_slackware_build # if build host is not slackware
	./bin/build-auxbuildroot 
}

make_cd ()
{
	if [ "$installer_for" = "gentoo" -a ! -e /etc/gentoo-release ]; then
		if [ ! "$build_host_gentoo_dir" -o ! -d "$build_host_gentoo_dir" ]; then
			echo -e "\n\nERROR: We need to be in a gentoo system to build or set build_host_gentoo_dir/build_host_dist\n\n"
			exit 1
		fi
	fi
	if [ ! -d $auxbuildroot_dir/FILES ]; then
		make_auxbuildroot
	fi
	$wd/bin/mk-dir-struct.sh
	chroot $auxbuildroot_dir ./BIN/build-nemopkg encdb
	if [ "$installer_for" = "slackware" ]; then
		chroot $auxbuildroot_dir ./BIN/build-nemopkg rngtools
	fi
	$wd/bin/build-installcd 	
	$wd/bin/build-initram 
	$wd/bin/build-nemopkg
	$wd/bin/build-kernel 

	$wd/bin/cp-pkgs-to-cdtree
	# docs, tools ...
	$wd/bin/cp-cdtree-files
	$wd/bin/make-iso
}

make_dsktool ()
{
	if [ ! -d $auxbuildroot_dir/FILES ]; then
		make_auxbuildroot
	fi
	./bin/mk-dir-struct.sh
	chroot $auxbuildroot_dir ./BIN/build-nemopkg encdb
	chroot $auxbuildroot_dir ./BIN/build-nemopkg rngtools
	./bin/build-dsktool
}

make_gentoo_dist ()
{
	if [ "$installer_for" != "gentoo" ]; then
		echo "Set installer_for=gentoo in ./env"
		return 1
	fi
	if [ ! -d $auxbuildroot_dir/FILES ]; then
		make_auxbuildroot
	fi
	$wd/bin/mk-dir-struct.sh
	$wd/bin/build-initram 
	$wd/bin/build-nemopkg 
	$wd/bin/cp-gentoo-dist
}

make_nemopkg ()
{
	if [ ! -d $auxbuildroot_dir/FILES ]; then
		make_auxbuildroot
	fi
	$wd/bin/build-nemopkg
	mkdir -p $wd/nemopkg
	$wd/bin/cp-pkgs.sh $wd/nemopkg
}

clean ()
{
	./bin/build-auxbuildroot clean
	rm -rf $wd/build/
	rm -rf $wd/cdtree/
	rm -rf $wd/gentoo-dist
	rm -rf $wd/gentoo-nemopkg
	rm -f $wd/bin/{installpkg,makepkg,tar-1.13,mkisofs,xz}
	./bin/mk-dir-struct.sh
}

clean_all ()
{
	clean
	rm -rf $wd/src
	rm -rf $wd/cdtree
	rm -f $wd/mostaza[64]-${installer_for}.iso
	./bin/mk-dir-struct.sh
}

make_release()
{
	local main_dir=$wd/release
	local cdtree_dir=$wd/cdtree/mostaza/nemo-a
	rm -rf $main_dir
	mkdir -p $main_dir/mostaza/{i486,x86_64}
	mkdir -p $main_dir/{dsktool,nemopkg/nemo-a/${slackware_arch},src,distfiles,installer_update}
	if [ "$installer_for" = gentoo ]; then
		cp $cdtree_dir/* $main_dir/distfiles/
		$wd/tools/mk-installer_update_tar_gz.sh 0
		cp $wd/installer_update-${mostaza_version}-${slackware_arch}-${installer_for}.tar.bz2 $main_dir/installer_update
		cp $wd/src/encdb-* $main_dir/src/
	elif [ "$installer_for" = slackware ]; then
		$wd/tools/mk-installer_update_tar_gz.sh 0
		cp $wd/installer_update-${mostaza_version}-${slackware_arch}-${installer_for}.tar.bz2 $main_dir/installer_update
		cp $cdtree_dir/* $main_dir/nemopkg/nemo-a/${slackware_arch}/
		cp $wd/src/encdb-* $main_dir/src/
	fi
}

# help
if [ ! "$1" -o "$1" = "-h" -o "$1" = "-help" ]; then
	echo "
	
	Build targets:

		Main targets:

			netinstall : makes a netinstall CD for Slackware or Gentoo.
			cdinstall : makes a CD for Slackware only.

		Optional targets:

			clean : remove all build generated directories
			cleanall : remove all build generated directories plust src,cdtree
			auxbuildroot : create auxbuildroot ( where everything else is buildt )
			nemopkg : build only slackware nemo packages
			dsktool : make dsktool utility.
			gentoo-dist : make repo tar and nemo tar if installer_for=gentoo
			release: make dir structure ready to upload to sourceforge, this should be
				ran after netinstall only.

"
	exit 1
fi

# root
if [ "$UID" != "0" ]; then
	echo "

	Must run as root!!
	
	I need to create the auxroot build env and then chroot to
	it, i will try not to harm your current environment.

"
	exit 1
fi

# main

echo -e "\n\nInstaller for: $installer_for"
echo "Conf Spec: $mostaza_conf_spec_version"
echo "Build host arch: $(uname -m)"
echo "Slackware build version: $slackware_version_build"
echo "Slackware target version: $slackware_version"
echo -e "Slackware arch: $slackware_arch \n\n"
sleep 1

if [[ "$(uname -m)" =~ i[3-6]86 && $slackware_arch = x86_64 ]]; then
	echo -e "Cannot build on this host for arch $slackware_arch \n"
	exit 1
fi

for c in $@ ; do
	case "$c" in
		'auxbuildroot')
			time make_auxbuildroot 2>&1 | tee ./tmp/build.log
			;;
		'netinstall')
			time make_cd 2>&1 | tee ./tmp/build.log
			;;
		'cdinstall')
			./tools/cp-slackware-sets.sh 2>&1 | tee ./tmp/build.log
			time make_cd 2>&1 | tee ./tmp/build.log
			;;
		'nemopkg')
			time make_nemopkg 2>&1 | tee ./tmp/build.log
			;;
		'dsktool')
			time make_dsktool 2>&1 | tee ./tmp/build.log
			;;
		'gentoo-dist')
			time make_gentoo_dist 2>&1 | tee ./tmp/build.log
			;;
		'cleanall')
			time clean_all 2>&1 | tee ./tmp/build.log
			;;
		'release')
			time make_release 2>&1 | tee ./tmp/build.log
			;;
		*)
			$@		
			;;
	esac
done
		
echo -e "\n MAKECD $1: DONE\n "
set +eET

# vim:ts=4:syn=sh:sw=4
# MAKECD: end
